package com.pentalog.ginet;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.pentalog.ginet.domain.Course;
import com.pentalog.ginet.domain.Student;

public class TestManyToMany {
   private static SessionFactory factory; 
   public static void main(String[] args) {
      try{
         factory = new Configuration().configure().buildSessionFactory();
      }catch (Throwable ex) { 
         System.err.println("Failed to create sessionFactory object." + ex);
         throw new ExceptionInInitializerError(ex); 
      }
      TestManyToMany ME = new TestManyToMany();
      HashSet<Course> course = new HashSet<Course>();
      course.add(new Course("Java Hibernate 3.0"));
      course.add(new Course(".Net"));
      course.add(new Course("PHP"));
      
      Integer empID1 = ME.addStudent("tlehuu", course);
     
//      Integer empID2 = ME.addStudent("mphamtuan", course);

      /* List down all the students */
      ME.listStudents();

      /* Update student's salary records */
//      ME.updateStudent(empID1, "pressf5");

      /* Delete an student from the database */
//      ME.deleteStudent(empID2);

      /* List down all the students */
//      ME.listStudents();

   }

   /* Method to add an student record in the database */
   public Integer addStudent(String name, Set<Course> courses){
      Session session = factory.openSession();
      Transaction tx = null;
      Integer studentID = null;
      try{
         tx = session.beginTransaction();
         Student student = new Student(name);
         student.setCourses(courses);
         studentID = (Integer) session.save(student); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
      return studentID;
   }

   /* Method to list all the students detail */
   public void listStudents( ){
      Session session = factory.openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         List<Student> students = session.createQuery("FROM Student").list(); 
         for (Iterator<Student> iterator1 = 
                           students.iterator(); iterator1.hasNext();){
            Student student = (Student) iterator1.next(); 
            System.out.println("\tFirst Name:\t" + student.getStudentName()); 
            Set<Course> certificates = student.getCourses();
            for (Iterator<Course> iterator2 = certificates.iterator(); iterator2.hasNext();){
                  Course certName = (Course) iterator2.next(); 
                  System.out.println("\tCourse:\t" + certName.getCourseName()); 
            }
         }
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
   }
   /* Method to update salary for an student */
   public void updateStudent(Integer StudentID, String name ){
      Session session = factory.openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         Student student = 
                    (Student)session.get(Student.class, StudentID); 
         student.setStudentName(name );
         session.update(student);
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
   }
   /* Method to delete an student from the records */
   public void deleteStudent(Integer StudentID){
      Session session = factory.openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         Student student = 
                   (Student)session.get(Student.class, StudentID); 
         session.delete(student); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
   }
}
